<template>
  <div class="app-container">
    <el-row :gutter="10">
      <!--树结构-->
      <el-col :span="4" :xs="24">
        <div class="app-container-tree-border">
          <el-tree
            :data="syTypeList"
            :props="defaultProps"
            :expand-on-click-node="false"
            ref="tree"
            node-key="id"
            highlight-current
            @node-click="handleNodeClick"
            :default-expand-all="defaultExpandAll"
          >
            <div slot-scope="{ node, data }" class="custom-tree-node" :title="node.label" v-text="node.label"></div>
          </el-tree>
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <div class="app-container-table-border">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
            <el-form-item label="表单名称" prop="excelTypeld">
              <el-select v-model="queryParams.excelTypeId" placeholder="请选择" @change="handleExcelTypeld">
                <el-option v-for="item in biExcelTypeList" :key="item.excelTypeId" :label="item.excelTypeName" :value="item.excelTypeId"> </el-option>
              </el-select>
            </el-form-item>
            <!-- <el-form-item label="当前试验室默认标识" prop="defaultFlag">
              <el-select v-model="queryParams.defaultFlag" placeholder="请选择当前试验室默认标识" clearable>
                <el-option v-for="dict in dict.type.default_flag" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item> -->
            <el-form-item label="是否系统默认" prop="sysYesNo">
              <el-select v-model="queryParams.sysYesNo" placeholder="请选择是否系统默认" clearable>
                <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>

          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5" v-if="queryParams.excelTypeId">
              <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['sycs:bsFanganInfo:add']">新增</el-button>
            </el-col>
            <!-- <el-col :span="1.5">
              <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['sycs:bsFanganInfo:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['sycs:bsFanganInfo:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['sycs:bsFanganInfo:export']">导出</el-button>
            </el-col> -->
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>

          <el-table v-loading="loading" :data="bsFanganInfoList" @selection-change="handleSelectionChange">
            <el-table-column label="方案名称" align="center" prop="fanganName" />
            <el-table-column label="当前试验室默认标识" align="center" prop="defaultFlag">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.default_flag" :value="scope.row.defaultFlag" />
              </template>
            </el-table-column>
            <el-table-column label="是否系统默认" align="center" prop="sysYesNo">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.sysYesNo" />
              </template>
            </el-table-column>
            <el-table-column label="备注" align="center" prop="remark" />
            <el-table-column label="" align="center" prop="syTypeId" />
            <el-table-column label="" align="center" prop="label" />
            <el-table-column label="操作" align="center" class-name="small-paddin" width="300">
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['sycs:bsFanganInfo:edit']">修改</el-button>
                <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['sycs:bsFanganInfo:remove']">删除</el-button>
                <el-button size="mini" type="text" icon="el-icon-thumb" @click="handleMoren(scope.row)" v-hasPermi="['sycs:bsFanganInfo:remove']">设为当前试验室默认</el-button>
                <!-- 当前试验室默认 -->
              </template>
            </el-table-column>
          </el-table>

          <!-- <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" /> -->
        </div>
      </el-col>
    </el-row>

    <!-- 添加或修改小数点方案管理对话框 -->
    <jy-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <!-- <el-form-item label="所属试验室id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入所属试验室id" />
        </el-form-item> -->
        <!-- <el-form-item label="当前试验室默认标识" prop="defaultFlag">
          <el-select v-model="form.defaultFlag" placeholder="请选择当前试验室默认标识">
            <el-option v-for="dict in dict.type.default_flag" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="excel类型id" prop="excelTypeId">
          <el-input v-model="form.excelTypeId" placeholder="请输入excel类型id" />
        </el-form-item> -->
        <el-row>
          <el-col :span="12">
            <el-form-item label="方案名称" prop="fanganName">
              <el-input v-model="form.fanganName" placeholder="请输入方案名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否系统默认" prop="sysYesNo">
              <el-select :disabled="title == '修改小数点方案管理'" v-model="form.sysYesNo" placeholder="请选择是否系统默认">
                <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <!-- <el-col :span="12">
            <el-form-item label="当前试验室默认" prop="defaultFlag">
              <el-select v-model="form.defaultFlag" placeholder="请选择当前试验室默认标识">
                <el-option v-for="dict in dict.type.default_flag" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
        <div style="width: 100%; text-align: right">
          <el-button round icon="el-icon-add" type="success" @click="addRow">新增行</el-button>
        </div>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column align="center" label="序号" type="index" width="50"> </el-table-column>
          <el-table-column align="center" prop="fieldLabel" label="字段名称">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fieldLabel" placeholder="请输入字段名称"></el-input>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="fieldKey" label="字段Key值">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fieldKey" placeholder="请输入字段Key值"></el-input>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="xsdws" label="保留几位小数">
            <template slot-scope="scope">
              <el-select v-model="scope.row.xsdws" placeholder="请选择保留几位小数">
                <el-option v-for="dict in dict.type.xsdws" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作">
            <template slot-scope="scope">
              <el-button @click="delRow(scope.row)" type="danger" slot="reference" size="mini">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </jy-dialog>
  </div>
</template>

<script>
import { listBsFanganInfo, getBsFanganInfo, delBsFanganInfo, addBsFanganInfo, updateBsFanganInfo, setDefault, getYusheInfo } from "@/api/sycs/bsFanganInfo";
import { syTpyeTree } from "@/api/base/syType";
import { listBiExcelType } from "@/api/syrw/biExcelType";
import { mapGetters } from "vuex";

export default {
  name: "BsFanganInfo",
  dicts: ["default_flag", "sys_yes_no", "xsdws"],
  data() {
    return {
      tableData: [],
      defaultProps: {
        children: "children",
        label: "label"
      },
      defaultExpandAll: true,
      // 试验类型列表
      syTypeList: [],
      biExcelTypeList: [],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 小数点方案管理表格数据
      bsFanganInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: undefined,
        excelTypeId: undefined,
        sysYesNo: undefined,
        fanganName: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fanganId: [{ required: true, message: "方案id不能为空", trigger: "blur" }],
        deptId: [{ required: true, message: "所属试验室id不能为空", trigger: "blur" }],
        excelTypeId: [{ required: true, message: "excel类型id不能为空", trigger: "blur" }],
        fanganName: [{ required: true, message: "方案名称不能为空", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getSyTypeTreeList();
  },
  mounted() {
    this.queryParams.deptId = this.deptId;
  },
  // 监听顶部全局下拉
  computed: {
    ...mapGetters(["deptId"])
  },
  watch: {
    deptId(newValue, oldValue) {
      this.queryParams.deptId = newValue;
    }
  },
  methods: {
    /** 查询试验类型树 */
    getSyTypeTreeList() {
      let param = {
        level: 2,
        innerFlag: "1"
      };
      syTpyeTree(param).then((response) => {
        this.syTypeList = response.data;
      });
    },
    /** 查询Excel类型管理列表 */
    getBdTypeList(id) {
      listBiExcelType({
        syTypeId: id,
        pageNum: 1,
        pageSize: 100
      }).then((response) => {
        this.biExcelTypeList = response.rows;
        this.queryParams.excelTypeId = response.rows[0].excelTypeId || null;
        this.getList();
      });
    },
    /** 查询小数点方案管理列表 */
    getList() {
      this.loading = true;
      listBsFanganInfo(this.queryParams).then((response) => {
        this.bsFanganInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleNodeClick(data) {
      // this.queryParams.syTypeId = data.id;
      // this.form.syTypeId = data.id;
      this.getBdTypeList(data.id);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fanganId: undefined,
        deptId: undefined,
        defaultFlag: undefined,
        excelTypeId: undefined,
        sysYesNo: undefined,
        fanganName: undefined,
        remark: undefined,
        syTypeId: undefined,
        label: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleExcelTypeld(val) {
      // console.log(val);
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.fanganId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getYusheInfoApi()

    },

    getYusheInfoApi() {
      this.tableData = [];
      getYusheInfo({ excelTypeId: this.queryParams.excelTypeId }).then((res) => {
        this.tableData = res.data || [];
        this.tableData.forEach((v, i) => {
          v.index = i + 1;
        });
        this.open = true;
        this.title = "添加小数点方案管理";
      });
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      this.tableData = [];
      const fanganId = row.fanganId || this.ids;
      getBsFanganInfo(fanganId).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.tableData = response.data.detailVos;
        this.tableData.forEach((v, i) => {
          v.index = i + 1;
        });

        this.open = true;
        this.title = "修改小数点方案管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.deptId = this.deptId;
          this.form.excelTypeId = this.queryParams.excelTypeId;
          this.form.detailVos = this.tableData;
          this.buttonLoading = true;
          if (this.form.fanganId != null) {
            updateBsFanganInfo(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addBsFanganInfo(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fanganIds = row.fanganId || this.ids;
      this.$modal
        .confirm('是否确认删除小数点方案管理编号为"' + fanganIds + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delBsFanganInfo(fanganIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    handleMoren(row) {
      const fanganIds = row.fanganId || this.ids;
      this.$modal
        .confirm(`是否确认把“${row.fanganName}”的数据项设为当前试验室默认吗？`)
        .then(() => {
          this.loading = true;
          return setDefault(fanganIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("试验室默认方案设置成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "sycs/bsFanganInfo/export",
        {
          ...this.queryParams
        },
        `bsFanganInfo_${new Date().getTime()}.xlsx`
      );
    },
    addRow() {
      const newRow = {
        fieldLabel: "",
        fieldKey: "",
        xsdws: "",
        index: this.tableData.length + 1
      };
      this.tableData.push(newRow);
    },
    delRow(row) {
      let valueToRemove = row.index;
      this.tableData = this.tableData.filter((v) => v.index !== valueToRemove);
    }
  }
};
</script>
